package com.fh.shop.api.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
@ApiModel("响应类")
public class ServerResponse implements Serializable {
    @ApiModelProperty("响应提示信息")
    private String msg;
    @ApiModelProperty(value = "响应编码", example = "0")
    private int code;
    @ApiModelProperty("响应数据")
    private Object data;

    private ServerResponse() {

    }

    private ServerResponse(String msg, int code, Object data) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static ServerResponse success() {
        return new ServerResponse(ResponseEnum.SUCCESS.getMsg(), ResponseEnum.SUCCESS.getCode(), null);
    }

    public static ServerResponse success(Object data) {
        return new ServerResponse(ResponseEnum.SUCCESS.getMsg(), ResponseEnum.SUCCESS.getCode(), data);
    }

    public static ServerResponse error(int code, String msg) {
        return new ServerResponse(msg, code, null);
    }

    public static ServerResponse error(ResponseEnum responseEnum) {
        return new ServerResponse(responseEnum.getMsg(), responseEnum.getCode(), null);
    }

    public static ServerResponse error(ResponseEnum responseEnum, String param) {
        String msg = responseEnum.getMsg();
        String newMsg = String.format(msg, param);
        return new ServerResponse(newMsg, responseEnum.getCode(), null);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
